<?php
namespace App\Service\Admin;

class BannerService extends BaseService
{
    //轮播图

    public static function index($type)
    {
        $db = self::dbConn(self::$dbBanner)->where('del', 0);
        if ($type) {
            $db = $db->where('type', $type);
        }
        $total = $db->count();
        $res = $db->orderBy('id', 'asc')->get();
        if (!count($res)) {
            return self::toback(0, '', [], $total);
        }
        $types = self::$cateTypes;
        foreach ($res as &$v) {
            $v->type_name = array_key_exists($v->type, $types) ? $types[$v->type] : '';
            $v->img = file_exists($v->img) ? env('DOMAIN') . '/' . $v->img : '';
        }
        return self::toback(0, '', $res, $total);
    }

    /**
     * 新增
     * @param array $pas
     */
    public static function add($pas)
    {
        $pas['addtime'] = toDate();
        $res = self::dbConn(self::$dbBanner)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    /**
     * 删除
     * @param int $id
     * return array
     */
    public static function del($id)
    {
        $res = self::dbConn(self::$dbBanner)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}